package com.coolsummer.aspect;

import com.coolsummer.abstracts.Limiter;
import com.coolsummer.enums.LimiterEnum;
import com.coolsummer.limiter.LimiterFactory;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author coolsummer
 * @describe 限流切面
 * @date 2019/11/21 16:36
 */
@Aspect
@Component
@Slf4j
public class LimiterAspect {

    @Pointcut("@annotation(com.coolsummer.annotions.LimiterAnnotion)")
    public void verify(){};

    @Before("verify()")
    public void doVerify(JoinPoint joinPoint){
        Limiter limiter = LimiterFactory.create(LimiterEnum.GUAVA_LIMITER, 100);
        if(limiter == null){
            throw new NullPointerException("redisLimit is null");
        }
        boolean limit = limiter.tryAcquire();
        if (!limit) {
            log.debug("request has bean limit");
        }
    }

}
